<?php

class ZFBlog_Form_CommentAdd extends ZFBlog_Form
{

    public function init()
    {
        $this->setAction('/entry/process');
        $this->setElementFilters(array('StringTrim'));

        // Configuration Data
        $config = Zend_Registry::get('configuration');

        // Display Group #1 : Comment Data

        $this->addElement('text', 'name', array(
            'decorators' => $this->_standardElementDecorator,
            'label' => 'Name:',
            'attribs' => array(
                'maxlength' => 60,
                'size' => 60
            ),
            'validators' => array(
                array('StringLength', false, array(3,60))
            ),
            'required' => true
        ));

        $this->addElement('text', 'email', array(
            'decorators' => $this->_standardElementDecorator,
            'label' => 'Email:',
            'attribs' => array(
                'maxlength' => 200,
                'size' => 60
            ),
            'validators' => array(
                array('StringLength', false, array(6,200)),
                array('EmailAddress')
            ),
            'required' => true
        ));

        $this->addElement('text', 'url', array(
            'decorators' => $this->_standardElementDecorator,
            'label' => 'Url:',
            'attribs' => array(
                'maxlength' => 200,
                'size' => 60
            ),
            'validators' => array(
                array('StringLength', false, array(12,200))
            ),
            'required' => true
        ));

        $this->addElement('textarea', 'content', array(
            'decorators' => $this->_standardElementDecorator,
            'label' => 'Comment:',
            'attribs' => array(
                'class'=>'comment',
                'rows'=>10
            ),
            'validators' => array(
                array('Akismet', false,
                    array(
                        $config->akismet->key,
                        'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']
                    )
                )
            ),
            'filters' => array('HtmlBody'),
            'required' => true
        ));

        $this->addElement('recaptcha', 'recaptcha_response_field', array(
            'decorators' => $this->_standardElementDecorator,
            'label' => 'Captcha:',
            'attribs' => array(
                'recaptchaParams' => array(
                    'privateKey'=>$config->recaptcha->privatekey,
                    'publicKey'=>$config->recaptcha->publickey,
                    'xhtml'=>true
                )
            ),
            'validators' => array(
                array('Recaptcha', false,
                    array(
                        $config->recaptcha->publickey,
                        $config->recaptcha->privatekey
                    )
                )
            ),
            'required' => true
        ));

        $this->addElement('hidden', 'id', array(
            'decorators' => $this->_noElementDecorator,
            'validators' => array(
                'Digits'
            ),
            'required' => true
        ));

        $this->addDisplayGroup(
            array('name','email','url','content','recaptcha_response_field','id'),
            'addcomment',
            array(
                'disableLoadDefaultDecorators' => true,
                'decorators' => $this->_standardGroupDecorator,
                'legend' => 'Add Comment'
            )
        );

        // Display Group #2 : Submit

        $this->addElement('submit', 'submit', array(
            'decorators' => $this->_buttonElementDecorator,
            'label' => 'Save'
        ));

        $this->addDisplayGroup(
            array('submit'), 'commentsubmit',
            array(
                'disableLoadDefaultDecorators' => true,
                'decorators' => $this->_buttonGroupDecorator,
                'class' => 'submit'
            )
        );

        // Leave the door open for HTTP options!
        if (Zend_Registry::isRegistered('httpOptions')) {
            $customClient = new Zend_Http_Client(null,
                Zend_Registry::get('httpOptions')->toArray()
            );
            ZFBlog_View_Helper_FormRecaptcha::setHttpClient(
                $customClient
            );
            ZFBlog_Validate_Recaptcha::setHttpClient(
                $customClient
            );
            ZFBlog_Validate_Akismet::setHttpClient(
                $customClient
            );
        }
    }
}